package mx.frantor.catalogos.daoimp;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import mx.frantor.catalogos.dao.ModulosDAO;
import mx.frantor.catalogos.entitys.ModulosEntity;
import mx.frantor.util.HibernateUtil;

public class ModulosDAOImpl implements ModulosDAO{

	@Override
	public String agregarModulos(ModulosEntity nuevoModulos) {
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
		
		session.beginTransaction();
		try {
		session.save(nuevoModulos);
		session.flush();
		session.getTransaction().commit();
		e = nuevoModulos.getIdModulo().toString();
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();
			//return e.toString();
		}
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		System.out.println("agregar " + e);
		return e;
	}

	@Override
	public String modificarModulos(ModulosEntity modificarModulos) {
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
	
		session.beginTransaction();
		try {
			ModulosEntity modulosTemp = (ModulosEntity) session.get(
					ModulosEntity.class, modificarModulos.getIdModulo());
		session.flush();
		session.evict(modulosTemp);
		session.update(modificarModulos);
		session.getTransaction().commit();
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();
			//return e.toString();
		}
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		System.out.println("modificar " + e);

		return e;
	}

	@Override
	public String eliminarModulos(Integer idModulos) {
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
		

			// TODO Auto-generated method stub			
			session.beginTransaction();
			try {
				ModulosEntity modulosElimina = (ModulosEntity) session.get(
						ModulosEntity.class, idModulos);
			session.delete(modulosElimina);
			session.getTransaction().commit();
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();
			//return e.toString();
		}
		//HibernateUtil.getSessionFactory().getCurrentSession().close();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		System.out.println("eliminar " + e);

		return e;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ModulosEntity> getTodosModulosActivos() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ModulosEntity> modulos = session.createCriteria(ModulosEntity.class)
				.addOrder(Order.asc("Nombre")).list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		return modulos;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ModulosEntity getModulosId(Integer idModulos) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ModulosEntity> modulo = (List<ModulosEntity>) session.createCriteria(ModulosEntity.class)
				.add(Restrictions.eq("IdModulo", idModulos))
				.setFetchSize(1).list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		if(modulo==null){
			return null;
		}else{
			return modulo.get(0);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ModulosEntity> getModulosPorId(Integer idModulos) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ModulosEntity> modulo = (List<ModulosEntity>) session.createCriteria(ModulosEntity.class)
				.add(Restrictions.eq("IdModulo", idModulos))
				.list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		return modulo;
	}

}
